package o;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import com.huawei.appgallery.packagemanager.impl.control.InstallService;
import java.util.ArrayList;
import java.util.List;

public class cox {
    private static cox ˊ;
    private static final Object ᐝ = new Object();
    private e ˋ = null;
    private Handler ˎ;
    private final List<Message> ˏ = new ArrayList();
    private InstallService ॱ;

    class b implements Runnable {
        final /* synthetic */ cox ˎ;
        private Context ˏ;
        private Runnable ॱ;

        private b(cox o_cox, Context context, Runnable runnable) {
            this.ˎ = o_cox;
            this.ॱ = runnable;
            this.ˏ = context.getApplicationContext();
        }

        public void run() {
            ˏ();
        }

        private void ˏ() {
            Message obtain = Message.obtain(this.ˎ.ˎ, this.ॱ);
            if (this.ˎ.ˊ(this.ˏ) != null) {
                obtain.sendToTarget();
                return;
            }
            synchronized (this.ˎ.ˏ) {
                this.ˎ.ˏ.add(obtain);
            }
        }
    }

    final class e implements ServiceConnection {
        final /* synthetic */ cox ॱ;

        private e(cox o_cox) {
            this.ॱ = o_cox;
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            try {
                this.ॱ.ॱ = ((com.huawei.appgallery.packagemanager.impl.control.InstallService.b) iBinder).ˋ();
                this.ॱ.ˊ();
                cnu.ˏ.ˋ("HiAppInstallServiceProxy", "Bind to InstallService sucessfuly");
            } catch (Throwable e) {
                cnu.ˏ.ˊ("HiAppInstallServiceProxy", "onServiceConnected error!", e);
            }
        }

        public void onServiceDisconnected(ComponentName componentName) {
            cnu.ˏ.ˋ("HiAppInstallServiceProxy", "ServiceDisconnected:" + componentName);
            this.ॱ.ॱ = null;
            this.ॱ.ˋ = null;
        }
    }

    public static cox ˋ(Context context) {
        cox o_cox;
        synchronized (ᐝ) {
            if (ˊ == null) {
                ˊ = new cox(context.getApplicationContext());
            }
            o_cox = ˊ;
        }
        return o_cox;
    }

    private cox(Context context) {
        this.ˎ = new Handler(context.getMainLooper());
    }

    public void ˊ(Context context, Runnable runnable) {
        new Handler(Looper.getMainLooper()).post(new b(context, runnable));
    }

    private void ˊ() {
        synchronized (this.ˏ) {
            for (Message sendToTarget : this.ˏ) {
                sendToTarget.sendToTarget();
            }
            this.ˏ.clear();
        }
    }

    private InstallService ˊ(Context context) {
        if (this.ॱ != null) {
            return this.ॱ;
        }
        ॱ(context);
        return null;
    }

    private boolean ॱ(Context context) {
        if (this.ˋ != null) {
            return true;
        }
        cnu.ˏ.ˋ("HiAppInstallServiceProxy", "bind to InstallService");
        Intent intent = new Intent(context, InstallService.class);
        this.ˋ = new e();
        return context.bindService(intent, this.ˋ, 1);
    }

    public void ˎ(Context context) {
        if (this.ˋ != null) {
            cnu.ˏ.ˋ("HiAppInstallServiceProxy", "unBind InstallService");
            context.unbindService(this.ˋ);
            this.ॱ = null;
            this.ˋ = null;
        }
    }
}
